# 用于输出genre.csv
from mongo import (
  collection_movie_info,
  get_all_data
)
from utils import (
  store_data_to_csv
)
def create_genre_csv():
  result = []
  # 1.拿到genre数据
  movie_data = get_all_data(collection_movie_info)
  # 2.根据每个id将genre数据依次变成a|b|c|d的形式
  for item in movie_data:
    if "genres" not in item:
      continue
    genre_list = item["genres"]
    # 2.1 提取genres的name并用"|"连接
    genres_str = "|".join(genre["name"] for genre in genre_list)
    # 2.2 创建新的数据结构
    new_data = {
        "movieId": item["id"],
        "genres": genres_str
    }
    result.append(new_data)
  store_data_to_csv(result, 'genres.csv')

create_genre_csv()